package com.example.pdfviewerplus;

import static java.lang.String.format;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;

import com.example.pdfviewerplus.R;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.ScrollBar;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;

// https://github.com/barteksc/AndroidPdfViewer
public class MainActivity extends Activity {
    private String dirPath = "/system/media";
    private String pdfName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		PDFView pdfView = (PDFView) findViewById(R.id.pdfView);
//		ScrollBar scrollBar = (ScrollBar) findViewById(R.id.scrollBar);
//		pdfView.setScrollBar(scrollBar);

		pdfName = getPDFName(dirPath).get(0);
        File pdfFile = new File(dirPath, pdfName);

		pdfView.fromFile(pdfFile).defaultPage(1).showMinimap(false)
				.enableSwipe(true).onLoad(new OnLoadCompleteListener() {
					@Override
					public void loadComplete(int nbPages) {
						Toast.makeText(MainActivity.this, "loadComplete",
								Toast.LENGTH_SHORT).show();
					}
				}).onPageChange(new OnPageChangeListener() {
					@Override
					public void onPageChanged(int page, int pageCount) {
						setTitle(format("%s %s / %s", pdfName, page, pageCount));
					}
				}).load();
	}
	
	public List<String> getPDFName(String path) {
        List<String> list = new ArrayList<String>();
        File file = new File(path);
        if (file.isDirectory()) {
            String pdfName[] = file.list();
            for (int i = 0; i < pdfName.length; i++) {
                if (pdfName[i].endsWith(".pdf") || pdfName[i].endsWith(".PDF")) {
                    list.add(pdfName[i]);
                }
            }
            return list;
        } else {
            return null;
        }
    }
}
